package com.fjec.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.market.entity.BizFollow;
import com.fjec.market.service.IBizFollowService;
import com.fjec.market.service.ISearchService;
import com.fjec.market.view.ShopVO;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/")
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @Resource
    private IBizFollowService bizFollowService;

    @GetMapping("/n/search")
    public JsonResult getSearchResult(Integer markCode, Integer target, String title, Page page) {
        Map<String, Object> m = new HashMap<>();
        switch (target) {
            case 1:
                m = searchService.getSearchResult(markCode, 1, 23, title, page);
                break;
            case 2:
                m = searchService.getSearchResult(markCode, 1, 5, title, page);
                break;
            case 3:
                m = searchService.getSearchResult(markCode, 1, 4, title, page);
                break;
            case 4:
                m = searchService.getSearchResult(markCode, 2, 6, title, page);
                break;
            case 5:
                m = searchService.getSearchResult(markCode, 2, 2, title, page);
                break;
            case 6:
                m = searchService.getSearchResult(markCode, 2, 3, title, page);
                break;
            case 7:
                m = searchService.getSearchResult(markCode, 2, 5, title, page);
                break;
            case 8:
                m = searchService.getSearchResult(markCode, 2, 4, title, page);
                break;
            default:
        }
        // 关注信息
        if (target == 4 || target == 5 || target == 6 || target == 7 || target == 8) {
            List<Map<String, Object>> shops = (List<Map<String, Object>>) m.get("data");
            Long thisUserId = SecurityUtils.getSubject().isAuthenticated() ? AccountInfo.getUserId() : null;
            if (thisUserId != null && thisUserId != 0) {
                List<BizFollow> follows = bizFollowService.list(new QueryWrapper<BizFollow>().eq("UserId", thisUserId));
                List<Long> followingIds = follows.stream().map(BizFollow::getBusiId).collect(Collectors.toList());
                if (followingIds.size() > 0) {
                    shops.forEach(s -> s.put("followed", followingIds.contains(MapUtils.getLong(s, "id"))));
                }
            }
        }
        return JsonResult.success().data(m).total(MapUtils.getLong(m, "total")).build();
    }
}
